package com.ctsi.djcloudmqtt.service;

import cn.hutool.core.util.StrUtil;
import com.ctsi.djcloudmqtt.dto.http.*;
import com.ctsi.djcloudmqtt.io.IwrkjServer;
import com.ctsi.djcloudmqtt.util.CommonConstants;
import com.ctsi.djcloudmqtt.util.json.jackson.JacksonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;

import java.io.IOException;
import java.util.UUID;

@Service
@Slf4j
public class WrkjService {

    @Value("${topicList.gatewaySn}")
    private String gatewaySn;

    @Value("${wrkj.url}")
    private String wrkjUrl;

    /**
     * 一键起飞
     * @return
     */
    public  DJHttpResponseParentDTO<Takeoff2pointResponseDTO> takeoff2point(){

        DJHttpRequestParentDTO djHttpRequestParentDTO = new DJHttpRequestParentDTO();
        djHttpRequestParentDTO.setBid(UUID.randomUUID().toString());
        djHttpRequestParentDTO.setTid(UUID.randomUUID().toString());
        djHttpRequestParentDTO.setTimestamp(System.currentTimeMillis());
        djHttpRequestParentDTO.setMethod(CommonConstants.DJServiceMethod.TAKEOFF_2_POINT);

        Takeoff2pointRequestDTO takeoff2pointRequestDTO = new Takeoff2pointRequestDTO();
        takeoff2pointRequestDTO.setFlightId(UUID.randomUUID().toString());
        takeoff2pointRequestDTO.setMaxSpeed(12);
        takeoff2pointRequestDTO.setOutOfControlAction(1);
        takeoff2pointRequestDTO.setRthAltitude(100);
        takeoff2pointRequestDTO.setSecurityTakeoffHeight(100);
        takeoff2pointRequestDTO.setTargetHeight(100);
        takeoff2pointRequestDTO.setTargetLatitude(32.29);
        takeoff2pointRequestDTO.setTargetLongitude(120.76);

        djHttpRequestParentDTO.setData(takeoff2pointRequestDTO);

        IwrkjServer iwrkjServer = this.getRetrofit();
        RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(djHttpRequestParentDTO));


        Call<ResponseBody> responseBodyCall =  iwrkjServer.takeoff2point(gatewaySn,body);
        try {
            Response<ResponseBody> callResponse = responseBodyCall.execute();
            if (callResponse.isSuccessful()) {
                String content = callResponse.body().string();
                log.info("一键起飞返回报文：{}", content);
                ObjectMapper mapper = new ObjectMapper();
                if (StrUtil.isNotEmpty(content)) {
                    return mapper.readValue(content, new TypeReference<DJHttpResponseParentDTO<Takeoff2pointResponseDTO>>() {});
                }
            }
        } catch (IOException e) {
            log.info("错误", e);
        }
        return null;
    }


    /**
     * 开启/关闭无人机视频车牌ai识别
     * @return
     */
    public  DJHttpResponseParentDTO changeAiStatus(DJHttpChangeAiStatusDTO djHttpChangeAiStatusDTO){
        DJChangeAiStatusRequestDTO djHttpRequestParentDTO = new DJChangeAiStatusRequestDTO();

        djHttpRequestParentDTO.setAiStatus(djHttpChangeAiStatusDTO.getAiStatus());
        djHttpRequestParentDTO.setUavId(111L);
        if(0L == djHttpChangeAiStatusDTO.getAiStatus()){
            djHttpRequestParentDTO.setAiKinds("[{\"dictCode\":236,\"dictName\":\"公安\",\"dictValue\":\"public_security\",\"children\":[{\"dictCode\":239,\"dictName\":\"车牌检测\",\"dictValue\":\"plateDetect\"}]}]");
        }
        djHttpRequestParentDTO.setPlateInfo(djHttpChangeAiStatusDTO.getPlateInfo());

        IwrkjServer iwrkjServer = this.getRetrofit();
        RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(djHttpRequestParentDTO));

        Call<ResponseBody> responseBodyCall =  iwrkjServer.changeAiStatus(body);
        try {
            Response<ResponseBody> callResponse = responseBodyCall.execute();
            if (callResponse.isSuccessful()) {
                String content = callResponse.body().string();
                log.info("开启/关闭无人机视频车牌ai识别返回报文：{}", content);
                if (StrUtil.isNotEmpty(content)) {
                    return JacksonUtil.string2Bean(content,DJHttpResponseParentDTO.class);
                }
            }
        } catch (IOException e) {
            log.info("错误", e);
        }
        return null;
    }

    /**
     *查询AI识别车牌结果
     * @param recordId
     * @return
     */
    public  DJHttpResponseParentDTO queryPlateInfo(Long recordId){
        IwrkjServer iwrkjServer = this.getRetrofit();

        Call<ResponseBody> responseBodyCall =  iwrkjServer.queryPlateInfo(recordId);
        try {
            Response<ResponseBody> callResponse = responseBodyCall.execute();
            if (callResponse.isSuccessful()) {
                String content = callResponse.body().string();
                log.info("查询AI识别车牌结果返回报文：{}", content);
                if (StrUtil.isNotEmpty(content)) {
                    return JacksonUtil.string2Bean(content,DJHttpResponseParentDTO.class);
                }
            }
        } catch (IOException e) {
            log.info("错误", e);
        }
        return null;
    }


    private IwrkjServer getRetrofit() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(wrkjUrl).build();
        return retrofit.create(IwrkjServer.class);
    }

}
